package com.ctsi.commons.util.crypto;

import com.ctsi.commons.util.GeneralException;

import javax.crypto.*;
import java.security.SecureRandom;

/**
 * java 或者默认jce似乎只支持128key java默认为ECB 模式,CBC不安全?,
 * 
 * @author yueming
 *
 */
public class AesCrypt implements SymmetryCrypt {

	private int deaultKeySize = 128;

	public void setDeaultKeySize(int deaultKeySize) {
		this.deaultKeySize = deaultKeySize;
	}

	private String algorithm = "AES";

	@Override
	public String getAlgorithm() {
		return algorithm;
	}

	public void setAlgorithm(String algorithm) {
		this.algorithm = algorithm;
	}

	private String transformation = "AES/ECB/PKCS5Padding";

	@Override
	public String getTransformation() {
		return transformation;
	}

	public void setTransformation(String transformation) {
		this.transformation = transformation;
	}
	@Override
	public int getDeaultKeySize() {

		return deaultKeySize;
	}
	
	
	@Override
	public SecretKey generateKey() throws Exception {
		return generateKey(getDeaultKeySize(), null);
	}

	@Override
	public SecretKey generateKey(int size, SecureRandom secr) throws Exception {
		KeyGenerator keyGen = KeyGenerator.getInstance(getAlgorithm());
		if (secr != null) {
			keyGen.init(size, secr); // 初始化
		} else {
			keyGen.init(size); // 初始化
		}

		// DES算法必须是56位
		// DESede算法可以是112位或168位
		// AES算法可以是128、192、256位
		// generate the AES key
		return keyGen.generateKey();
	}

	@Override
	public byte[] encrypt(SecretKey key, byte[] bytes) throws GeneralException {

		return encrypt(key, bytes, 0, bytes.length);
	}

	@Override
	public byte[] encrypt(SecretKey key, byte[] bytes, int off, int len) throws GeneralException {

		byte[] encBytes = null;
		try {
			Cipher cipher = Cipher.getInstance(transformation);
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			// Cipher cipher = DesCrypt.getCipher(key, Cipher.ENCRYPT_MODE);
			encBytes = cipher.doFinal(bytes, off, len);
		} catch (IllegalStateException e) {
			throw new GeneralException(e);
		} catch (IllegalBlockSizeException e) {
			throw new GeneralException(e);
		} catch (BadPaddingException e) {
			throw new GeneralException(e);
		} catch (Exception e) {
			throw new GeneralException(e);
		}
		return encBytes;
	}

	@Override
	public byte[] decrypt(SecretKey key, byte[] bytes, int off, int len) throws GeneralException {

		byte[] decBytes = null;
		try {
			Cipher cipher = Cipher.getInstance(transformation);// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			// Cipher cipher = DesCrypt.getCipher(key, Cipher.DECRYPT_MODE);
			decBytes = cipher.doFinal(bytes, off, len);
		} catch (IllegalStateException e) {
			throw new GeneralException(e);
		} catch (IllegalBlockSizeException e) {
			throw new GeneralException(e);
		} catch (BadPaddingException e) {
			throw new GeneralException(e);
		} catch (Exception e) {
			throw new GeneralException(e);
		}
		return decBytes;
	}

	@Override
	public byte[] decrypt(SecretKey key, byte[] bytes) throws GeneralException {

		return decrypt(key, bytes, 0, bytes.length);
	}



}
